﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Microsoft.MediaCenter.UI;
using MyVideoViewerLibrary;

namespace MyVideoViewerMCPlugin
{
    public class MovieMenu : ContentPage
    {
        //private MovieDataInterface movieData;
        private IMoviesBusiness movieBL;
        internal int? chosenOption;        

        public MovieMenu()
        { }
        public MovieMenu(int? menuOption)
        {
            movieBL = MoviesBusiness.MoviesBL;
            //movieData = new MyMoviesData();
            //Index.Value = 0;

            Content = new VirtualList(this, null);

            if (!menuOption.HasValue)
            {
                foreach (CommandItem item in movieBL.GetCommandItems())
                {
                    AddCommand(item.Description, item.Data);
                }
            }
            else
            {
                chosenOption = menuOption;
                foreach (CommandItem item in movieBL.GetCommandItems(menuOption.Value))
                {
                    AddCommand(item.Description, item.Data);
                }                
            }
        }

        void command_Invoked(object sender, EventArgs e)
        {
            MenuCommand command = sender as MenuCommand;
            
            List<MyMovie> myMovies = new List<MyMovie>();

            if (chosenOption.HasValue)
            {
                List<Movie> movies = movieBL.GetMoviesFromCommand(chosenOption.Value, command.Data);

                foreach (Movie movie in movies)
                    myMovies.Add(new MyMovie(movie));

                Application.Current.GoToGalleryPage(new MovieGallery(myMovies));
                return;
            }
            else
            {
                int? id = (int?)command.Data;
                if (id == 0)
                {
                    List<Movie> movies = movieBL.GetMoviesFromCommand(id.Value, null);
                    foreach (Movie movie in movies)
                        myMovies.Add(new MyMovie(movie));
                    Application.Current.GoToGalleryPage(new MovieGallery(myMovies));
                    return;
                }
                Application.Current.GoToMovieMenu(new MovieMenu(id));
            }
        }

        internal void AddCommand(string desc, object data)
        {
            MenuCommand command = new MenuCommand() { Description = desc, Owner = this, Data = data };
            command.Invoked += new EventHandler(command_Invoked);
            Content.Add(command);
        }

        public override int JumpTo(string displayValue)
        {
            displayValue = displayValue.ToLower();
            List<string> commands = Content.Cast<MenuCommand>().Select(c => c.Description).ToList();
            List<string> temp = new List<string>();
            int x = -1;
            do
            {
                for (int i = 0; i < commands.Count; i++)
                {
                    if (commands[i].ToLower().StartsWith(displayValue))
                    {
                        x = i;
                        break;
                    }
                }

                if (displayValue.Length > 0)
                    displayValue = displayValue.Substring(0, displayValue.Length - 1);
            } while (x < 0 && displayValue.Length > 0);

            if (x >= 0)
                return x;
            return -1;
        }
    }
}
